<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script src="./js/jquery-1.7.2.min.js"></script>
</head>
<style>
.fontClass{
	font-size : 20px;
	font-weight : blod;
	letter-spacing : 8px;
}

div.line{
height:5px;
border-top:2px solid black;
width:100%;
}

.shuTestItem{
	margin:auto;
}

</style>
<script>
window. addEventListener ('message', function(event) { 
	//event.data获取传过来的数据
	if(event.data.type == "print"){
		printTest();
	}else if(event.data.type == "test"){
		generate(event.data.testData);
	}
});

function printTest(){
  window.print(); 
}

//判断计算题目是否重复
//只是字符串比较 1+2 和2+1 为false
var maxCacheCount = 60; //多少个以内不重复
var resultCache = []
function isExist(testItem){
	for(var i = 0;i<resultCache.length;i++){
		if(resultCache[i] == testItem){
		//console.log(testItem);
			return true;
		}else{
			resultCache.push(testItem);
			if(resultCache.length>maxCacheCount){
				resultCache.unshift();
			}
		}
	}
	return false;
}

//生成计算题目
function generate(options){
	$("#testList").empty();
	
	if(options.totalWeight >0){
		var result = [];
		var sumCount = options.total;
		
		while(sumCount >0 ){
			//在总的权重下面,得到一个随机数
			//通过随机数,得到当前需要整哪个计算
			var random = Math.round(Math.random() * options.totalWeight);
			var number = 0;
			
			for(var i = 0 ; i <= options.item.length -1;i++){
				if(random <= options.item[i].weight)
				{
					number = i;
					break;
				}
			}
			var targetScript = options.item[number].method+"();";
			var testItem = eval(targetScript);
			
			//判断是否重复
			if(!isExist(testItem)){
				result.push(testItem);
				sumCount --;	
			}
		}
		
		//开始渲染题目表格
		var table = $("<table></table>").addClass("testTable").attr("width","100%");
		var colCount = 0;
		var lastTr;
		$.each(result,function(i,n){
			colCount = i % options.colNum;
			if(colCount == 0){
				lastTr = $("<tr></tr>").addClass("testTableRow");
				table.append(lastTr);
			}
			
			var col = showTestItem(n);
			var td = $("<td></td>");
			td.append(col);
			lastTr.append(td);
		});
		
		$("#testList").append(table);
	}
}

/**
*	生成单个的计算题目
*/
function showTestItem(testItem){
	
	var testTable = $("<table></table>").addClass("shuTestItem").attr("width","100px");
	var tr = $("<tr></tr>");
	tr.append($("<td></td>"));
	tr.append($("<td></td>").attr("align","right").addClass("fontClass").text(testItem.left));
	testTable.append(tr);
	var tr2 = $("<tr></tr>");
	tr2.append($("<td></td>").attr("align" , "left").addClass("fontClass").text(testItem.flag));
	tr2.append($("<td></td>").attr("align" , "right").addClass("fontClass").text(testItem.right));
	testTable.append(tr2);
	var tr3 = $("<tr></tr>");
	tr3.append($("<td></td>").attr("colspan" , 2).append($("<div></div>").addClass("line")));
	testTable.append(tr3);
	
	var tr4 = $("<tr></tr>").attr("height","50px");
	testTable.append(tr4);
	//$("#listTestList").append(testTable);
	return testTable;
}


//--------------------------------------计算方法

//20以内加减法
function innerTwenty(){
	var flag = Math.round(Math.random() * 1000);
	var one , two , flagStr;
	if(flag % 2 == 0){
		//加法
		flagStr = "+";
		two = 1+Math.round(Math.random() * 8);
		one = 1+Math.round(Math.random() * 8);
		
	}else{
		flagStr = "-";
		var number1 = 1+Math.round(Math.random() * 8);
		var number2 = 10+Math.round(Math.random() * 9);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}

function innerFifty(){
	var flag = 1+Math.round(Math.random());
	var one , two , flagStr;
	if(flag>1){
		//加法
		flagStr = "✚";
		two = 10+Math.round(Math.random() * 40);
		one = 10+Math.round(Math.random() * 40);
		
	}else{
		flagStr = "一";
		var number1 = 10+Math.round(Math.random() * 40);
		var number2 = 10+Math.round(Math.random() * 40);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}

function innerHundred(){
	var flag = 1+Math.round(Math.random());
	var one , two , flagStr;
	if(flag>1){
		//加法
		flagStr = "✚";
		two = 10+Math.round(Math.random() * 90);
		one = 10+Math.round(Math.random() * 90);
		
	}else{
		flagStr = "一";
		var number1 = 10+Math.round(Math.random() * 90);
		var number2 = 10+Math.round(Math.random() * 90);
		one = number1>number2?number1:number2;
		two = number1<number2?number1:number2;
	}
	return {
	left:one,flag:flagStr,right:two
	}
}

function inner20Multi(){
	var flagStr = "╳";
	var one , two , flagStr;
	
	two = 10+Math.round(Math.random() * 10);
	one = 10+Math.round(Math.random() * 10);
		
	return {
	left:one,flag:flagStr,right:two
	}
}

function one10Multi(){
	var flagStr = "╳";
	var one , two , flagStr;
	
	two = 1+Math.round(Math.random() * 8);
	one = 9+Math.round(Math.random() * 10);
		
	return {
	left:one,flag:flagStr,right:two
	}
}

</script>
<style>
body{overflow-x：hidden；overflow-y：auto；}

</style>

<body>
<div id="testList"></div>
</body>
</html>